package com.honey.mamabike.common.resp;

import com.honey.mamabike.cache.CommonCacheUtil;
import com.honey.mamabike.common.constants.Constants;
import com.honey.mamabike.user.entity.UserElement;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by setsun on 2017/10/26.
 */
@Slf4j
public class BaseController {

    @Autowired
    private CommonCacheUtil cacheUtil;

    /**
     * 获取缓存token的ID
     * @return
     */
    protected UserElement getCurrentUser(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
       String token = request.getHeader(Constants.REQUEST_TOKEN_KEY);
       if (!StringUtils.isBlank(token)){
           try{
               UserElement userElement = cacheUtil.getUserByToken(token);
               return userElement;
           }catch (Exception e ){
               log.error("Fail to get user by token",e);
               throw e;
           }
       }
        return null;
    }


}
